package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import util.DBHelper;

public class UserCollectionDAO {
	
	public boolean selectCollectByTPID(int teacherID, String tpid) throws SQLException {
		Connection conn;
		Statement stmt;
		ResultSet rs;
		
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();

		rs = stmt.executeQuery("select count(*) from usercollection where TeacherID = " + teacherID + " and TPID = '" + tpid + "'");
		rs.next();
		
		boolean flag = rs.getInt(1) == 0 ? false : true;
		rs.close();
		stmt.close();
		conn.close();
		return flag;

	}
	
	public void collectByTPID(int teacherID, String tpid) throws SQLException {
		Connection conn = DBHelper.getConnection();
		Statement stmt;
		
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		stmt.execute("insert into usercollection values(" + teacherID + ", '" + tpid + "')");
		
		stmt.close();
		conn.close();
	}
	
	public void undoCollectByTPID(int teacherID, String tpid) throws SQLException {
		Connection conn = DBHelper.getConnection();
		Statement stmt;
		
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		stmt.execute("delete from usercollection where TeacherID = " + teacherID + " and TPID = '" + tpid + "'");
		
		stmt.close();
		conn.close();
	}
}
